package com.example.meituan.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.meituan.activity.bean.UserBean;
import com.example.meituan.db.DBUntil;

import java.util.ArrayList;

public class UserDao {
    private static SQLiteDatabase db= DBUntil.con;



    // 插入用户
    public long insertUser(UserBean user) {
        db= DBUntil.con;
        ContentValues values = new ContentValues();
        values.put("s_id", user.getsId());
        values.put("s_pwd", user.getsPwd());
        values.put("s_name", user.getsName());
        values.put("s_phone", user.getsPhone());
        values.put("s_shen", user.getsShen());
        values.put("s_img", user.getsImg());

        return db.insert("d_user", null, values);
    }

    // 更新用户信息
    public static int updateUser(UserBean user) {
        db= DBUntil.con;
        ContentValues values = new ContentValues();
        values.put("s_pwd", user.getsPwd());
        values.put("s_name", user.getsName());
        values.put("s_phone", user.getsPhone());
        values.put("s_shen", user.getsShen());
        values.put("s_img", user.getsImg());

        return db.update("d_user", values, "s_id=?", new String[]{user.getsId()});
    }

    // 删除用户
    public static int deleteUser(String userId) {
        db= DBUntil.con;
        return db.delete("d_user", "s_id=?", new String[]{userId});
    }

    // 获取单个用户
    public static UserBean getUserById(String userId) {
        db= DBUntil.con;
        Cursor cursor = db.query("d_user", null, "s_id=?", new String[]{userId}, null, null, null);

        UserBean user = UserBean.fromCursor(cursor);
        cursor.close();
        return user;
    }

    // 获取所有用户
    public ArrayList<UserBean> getAllUsers() {
        db= DBUntil.con;
        Cursor cursor = db.query("d_user", null, null, null, null, null, null);

        ArrayList<UserBean> users = UserBean.fromCursorList(cursor);
        cursor.close();
        return users;
    }



}